<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-if="formThead.id" v-model="listQuery.id" style="width: 200px;" class="filter-item" placeholder="id" clearable @keyup.enter.native="handleFilter"/>
      <div v-if="formThead.status" class="filter-item">
        <el-select v-model="listQuery.status" placeholder="状态" clearable @keyup.enter.native="handleFilter">
          <el-option v-for="op in select_status" :key="op.value" :label="op.label" :value="op.value"/>
        </el-select>
      </div>
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">搜索</el-button>
      <addPage @getLists="getLists"/>
    </div>

    <div class="filter-container">
      <el-checkbox v-model="formThead.id" label="id">id</el-checkbox>
      <el-checkbox v-model="formThead.class_name" label="分类名">分类名</el-checkbox>
      <el-checkbox v-model="formThead.sort" label="排序值">排序值</el-checkbox>
      <el-checkbox v-model="formThead.nickname" label="名字">名字</el-checkbox>
      <el-checkbox v-model="formThead.status" label="状态">状态</el-checkbox>
    </div>

    <el-table v-loading="listLoading" :key="tableKey" :data="list" border fit highlight-current-row style="width: 100%;">
      <el-table-column v-if="formThead.id" label="id" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column v-if="formThead.class_name" label="类名" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.class_name }}</span>
        </template>
      </el-table-column>
      <el-table-column v-if="formThead.sort" label="排序值" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.sort }}</span>
        </template>
      </el-table-column>
      <el-table-column v-if="formThead.nickname" label="名字" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.nickname }}</span>
        </template>
      </el-table-column>
      <el-table-column v-if="formThead.status" label="状态" align="center">
        <template slot-scope="scope">
          <!--<span v-if="scope.row.status == ''">—</span>-->
          <span v-if="scope.row.status == 0"><el-button type="warning" plain size="mini">禁用</el-button></span>
          <span v-if="scope.row.status == 1"><el-button type="success" plain size="mini">正常</el-button></span>
          <span v-if="scope.row.status == 2"><el-button type="warning" plain size="mini">待审核</el-button></span>
          <span v-if="scope.row.status == 3"><el-button type="warning" plain size="mini">审核未通过</el-button></span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <!--编辑-->
          <editPage :row_data="scope.row" @getLists="getLists"/>
          <!--删除-->
          <el-button type="danger" icon="el-icon-delete" circle @click="del(scope.row.id)"/>
          <!--通过-->
          <div style="display: inline-block;">
            <el-button v-if="scope.row.status == 2" type="success" icon="el-icon-delete" circle @click="pass(scope.row.id)"/>
          </div>
          <!--不通过-->
          <div style="display: inline-block;">
            <el-button v-if="scope.row.status == 2" type="warning" icon="el-icon-delete" circle @click="notPass(scope.row.id)"/>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <!--大图弹窗-->
    <transition name="el-zoom-in-top">
      <div v-show="bigImageSrc !== null" class="bigImg" @click="bigImage">
        <img :src="bigImageSrc" style="width:auto; height:400px; border-radius:0px;">
      </div>
    </transition>
    <!--页码-->
    <div class="pagination-container">
      <el-pagination :current-page="listQuery.page" :page-sizes="[10,20,30, 50]" :page-size="listQuery.limit" :total="total" background layout="total, sizes, prev, pager, next, jumper" @size-change="handleSizeChange" @current-change="handleCurrentChange"/>
    </div>
  </div>
</template>

<script>
import { getList, del, edit } from '@/api/article/category'
import store from '@/store'
import waves from '@/directive/waves' // 水波纹指令
import addPage from './addPage'
import editPage from './editPage'

export default {
  name: 'ComplexTable',
  directives: {
    waves
  },
  components: { addPage, editPage },

  data() {
    return {
      tableKey: 0,
      list: null,
      total: null,
      listLoading: true,
      bigImageSrc: null,
      select_type: [],
      select_status: [
        { value: 0, label: '禁用' },
        { value: 1, label: '正常' }
      ],
      listQuery: {
        id: undefined,
        class_name: undefined,
        sort: undefined,
        parent_id: undefined,
        nickname: undefined,
        creater: undefined,
        updater: undefined,
        status: undefined,
        create_time: undefined,
        update_time: undefined,
        page: 1,
        limit: 20
      },
      formThead: {
        id: true,
        class_name: true,
        sort: true,
        nickname: true,
        creater: true,
        updater: false,
        status: true,
        create_time: true,
        update_time: false
      }
    }
  },
  created() {
    this.getLists()
  },
  methods: {
    getLists() {
      this.listLoading = true
      getList(store.getters.token, this.listQuery).then(res => {
        if (res.data.status) {
          this.list = res.data.list.data
          this.total = res.data.list.total
        } else {
          this.$message.error(res.data.msg)
        }
        this.listLoading = false
      })
    },
    handleEdit(row) {
      const data = row
      edit(store.getters.token, data).then(res => {
        console.log(res)
        if (res.data.status) {
          this.$message.success('修改成功！')
        } else {
          this.$message.error('修改失败！原因：' + res.data.msg)
        }
      })
    },
    bigImage(src) {
      if (src > 0) {
        this.set(this, 'bigImageSrc', src)
      } else {
        this.set(this, 'bigImageSrc', null)
      }
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getLists()
    },
    handleSizeChange(val) {
      this.listQuery.limit = val
      this.getLists()
    },
    handleCurrentChange(val) {
      this.listQuery.page = val
      this.getLists()
    },
    del(id) {
      this.$confirm('此操作将永久删除该文章, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$message.info('正在删除，ID：' + id)
        del(store.getters.token, { id: id }).then(res => {
          if (res.status) {
            this.$message.success('已经删除，ID：' + id)
            this.getLists()
          } else {
            this.$message.error('删除失败，ID：' + id)
          }
        })
      }).catch(() => {
        this.$message.info('已取消删除，ID：' + id)
      })
    }
  }
}

</script>
